package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.vo.AttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupVo> queryAttrgroupWithAttrsByCid(String categoryId) {

        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, categoryId));
        List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>()
                .eq(AttrEntity::getType, 1));
        List<AttrGroupVo> attrGroupVos = attrGroupEntities.stream()
                .map(attrGroupEntity -> {
                    AttrGroupVo attrGroupVo = new AttrGroupVo();
                    BeanUtils.copyProperties(attrGroupEntity, attrGroupVo);
                    attrGroupVo.setAttrEntities(attrEntities.stream().filter(attrEntity ->
                            attrEntity.getGroupId() == attrGroupEntity.getId())
                            .collect(Collectors.toList()));
                    return attrGroupVo;
                }).collect(Collectors.toList());
        return attrGroupVos;
    }

}